home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / computerjanitor / plugins / dpkg_status_plugin.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  2.2 KB  |  67 lines

  1. # dpkg_status.py - compact the dpkg status file
  2. # Copyright (C) 2009  Canonical, Ltd.
  3. #
  4. # Author: Michael Vogt <mvo@ubuntu.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, version 3 of the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  
  18.  
  19. from apt_pkg import ParseTagFile, PkgSystemLock, PkgSystemUnLock
  20. import subprocess
  21. import logging
  22.  
  23. import computerjanitor
  24. _ = computerjanitor.setup_gettext()
  25.  
  26.  
  27. class DpkgStatusCruft(computerjanitor.Cruft):
  28.  
  29.     def __init__(self, n_items):
  30.         self.n_items = n_items
  31.  
  32.     def get_prefix(self):
  33.         return "dpkg-status"
  34.  
  35.     def get_prefix_description(self): # pragma: no cover
  36.         return _("%i obsolete entries in the status file") % self.n_items
  37.  
  38.     def get_shortname(self):
  39.         return _("Obsolete entries in dpkg status")
  40.  
  41.     def get_description(self): # pragma: no cover
  42.         return _("Obsolete dpkg status entries")
  43.  
  44.     def cleanup(self): # pragma: no cover
  45.         logging.debug("calling dpkg --forget-old-unavail")
  46.         res = subprocess.call(["dpkg","--forget-old-unavail"])
  47.         logging.debug("dpkg --forget-old-unavail returned %s" % res)
  48.  
  49. class DpkgStatusPlugin(computerjanitor.Plugin):
  50.  
  51.     def __init__(self, fname="/var/lib/dpkg/status"):
  52.         self.status = fname
  53.         self.condition = ["PostCleanup"]
  54.     
  55.     def get_cruft(self):
  56.         n_cruft = 0
  57.         tagf = ParseTagFile(open(self.status))
  58.         while tagf.Step():
  59.             statusline = tagf.Section.get("Status")
  60.             (want, flag, status) = statusline.split()
  61.             if want == "purge" and flag == "ok" and status == "not-installed":
  62.                 n_cruft += 1
  63.         logging.debug("DpkgStatusPlugin found %s cruft items" % n_cruft)
  64.         if n_cruft:
  65.             return [DpkgStatusCruft(n_cruft)]
  66.         return [] # pragma: no cover
  67.